﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using System.IO;
namespace QUANLYDIEMHOCSINH
{
    public partial class xemxeploaihs : Form
    {
        LOPBUS lopbus = new LOPBUS();
        NAMHOCBUS nambus = new NAMHOCBUS();
        HOCKYBUS hkbus = new HOCKYBUS();
        DIEMBUS diembus = new DIEMBUS();
        public xemxeploaihs()
        {
            InitializeComponent();
            set_AN();
            LoadDanhSachLop();
            LoadNamHoc();
            LoadDanhSachHocKy();
            LoadDanhSachXepLoai(int.Parse(cbHocKy.SelectedValue.ToString()), int.Parse(cbLop.SelectedValue.ToString()));
        }
        void set_AN()
        {
            if (File.Exists("AnhNen.txt"))
            {
                FileStream fs = new FileStream("AnhNen.txt", FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string s = null;
                string t = null;
                while ((s = sr.ReadLine()) != null)
                {
                    t = s;
                }
                this.BackColor = Color.FromName(t);
                fs.Close();
            }
            else
            {
                FileStream fs = new FileStream("AnhNen.txt", FileMode.Append);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine("Turquoise");
                sw.Flush();
                sw.Close();
                fs.Close();

                FileStream fs1 = new FileStream("AnhNen.txt", FileMode.Open);
                StreamReader sr1 = new StreamReader(fs1);
                string s = null;
                string t = null;
                while ((s = sr1.ReadLine()) != null)
                {
                    t = s;
                }
                this.BackColor = Color.FromName(t);
                fs1.Close();
            }
        }
        void LoadDanhSachLop()
        {
            cbLop.DataSource = lopbus.layDSL();
            cbLop.DisplayMember = "TENLOP";
            cbLop.ValueMember = "MALOP";
            cbLop.SelectedItem = 0;
        }

        void LoadNamHoc()
        {
            NAMHOCDTO namhoc = nambus.LoadNamHocHienTai();
            txtNamHoc.Text = namhoc.NAMBATDAU + " - " + namhoc.NAMKETTHUC;
        }
        void LoadDanhSachHocKy()
        {
            cbHocKy.DataSource = hkbus.LoadDachHocKy();
            cbHocKy.DisplayMember = "TENHK";
            cbHocKy.ValueMember = "MAHK";
            cbHocKy.SelectedIndex = 0;
        }
        void LoadDanhSachXepLoai(int mahk, int malop)
        {
            dtgrwBangXepLoai.DataSource = diembus.DanhSachXepLoaiHocSinh(mahk, malop);
            for (int i = 0; i < dtgrwBangXepLoai.Rows.Count;i++ )
            {
                dtgrwBangXepLoai.Rows[i].Cells[0].Value = (i + 1).ToString();
            }
            dtgrwBangXepLoai.Refresh();
        }
        private void cbLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDanhSachXepLoai(int.Parse(cbHocKy.SelectedValue.ToString()), int.Parse(cbLop.SelectedValue.ToString()));
        }

        private void cbHocKy_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDanhSachXepLoai(int.Parse(cbHocKy.SelectedValue.ToString()), int.Parse(cbLop.SelectedValue.ToString()));
        }

        private void dtgrwBangXepLoai_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            dtgrwBangXepLoai.Rows[e.RowIndex].Cells["stt"].Value = e.RowIndex + 1;
        }

        private void btinbangxeploai_Click(object sender, EventArgs e)
        {
            frmXeploai xp = new frmXeploai();
            xp.MaHocKy = int.Parse(cbHocKy.SelectedValue.ToString());
            xp.MaLop = int.Parse(cbLop.SelectedValue.ToString());
            xp.NamHoc = txtNamHoc.Text;
            xp.HocKy = cbHocKy.Text;
            xp.Lop = cbLop.Text;
            xp.ShowDialog();   
        }
    }
}
